package com.wedu.view;

import com.alibaba.druid.sql.PagerUtils;
import com.wedu.entity.Student;
import com.wedu.service.StudentService;
import com.wedu.service.impl.StudentServiceImpl;
import org.junit.Test;

import java.util.Scanner;

//用户界面UI
public class Login {
    private StudentService studentService = new StudentServiceImpl();
    private Scanner sc = new Scanner(System.in);

    //登录测试

    public void login() {
        //1.键盘录入，接收用户名和密码
        System.out.println("请输入用户名：");
        String name = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        //2.调用方法
        boolean flag = studentService.loginStu(name, password);
        //3.判断结果，输出不同语句
        if (flag) {
            //登录成功
            System.out.println("登录成功！");
        } else {
            System.out.println("用户名或密码错误！");
        }
    }


    public void register(){
        System.out.println("请输入姓名：");
        String name=sc.nextLine();
        System.out.println("请输入性别：");
        String sex=sc.nextLine();
        System.out.println("请输入年龄：");
        int age= studentService.inputNumber();
        System.out.println("请输入密码：");
        String password=sc.nextLine();

        boolean flag= studentService.register(name,sex,age,password);
        if (flag){
            System.out.println("注册成功");
        }else {
            System.out.println("注册失败");
        }
    }


    public Student findOneById(){
        while (true) {
            System.out.println("请输入id：");
            int id = studentService.inputNumber();
            Student student = studentService.findOneById(id);
            if (student != null) {
                return student;
            }else {
                System.out.println("输入的id不正确，请重新输入");
            }
        }
    }


    public Student deleteStu(){
        System.out.println("请输入删除学生的id：");
        int id= studentService.inputNumber();
        return studentService.deleteStu(id);
    }


    public Student updateMsg(){
        while (true) {
            System.out.println("请输入修改学生的id：");
            int id = studentService.inputNumber();
            System.out.println("请输入姓名：");
            String name = sc.nextLine();
            System.out.println("请输入性别：");
            String sex = sc.nextLine();
            System.out.println("请输入年龄：");
            int age = studentService.inputNumber();
            System.out.println("请输入密码：");
            String password = sc.nextLine();

            Student s = studentService.updateMsg(id, name, age, sex, password);
            if (s != null) {
                return s;
            }
        }
    }
}
